﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto.Home;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteUtil.Attributes;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Web
{
    [Route("api/web/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(LanguageHeaderFilter))]
    public class HomeController : ControllerBase
    {
        private readonly GlobalConfigService _globalConfigService;
        private readonly AboutUsService _aboutUsService;
        private readonly LangSplitHelper _langSplitHelper;
        private readonly Mapper _mapper;


        public HomeController(LangSplitHelper langSplitHelper, Mapper mapper, GlobalConfigService globalConfigService, AboutUsService aboutUsService)
        {
            _langSplitHelper = langSplitHelper;
            _mapper = mapper;
            _globalConfigService = globalConfigService;
            _aboutUsService = aboutUsService;
        }

        /// <summary>
        /// 获取公司基本信息
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetCompanyInfo(LangType language)
        {
            try
            {
                CompanyDTO companyDTO;
                CompanyAboutUsDTO companyInfo;
                List<CompanyAboutUsDTO> companyAboutUsDTOs;
                CompanyInfoDTO companyInfoDTO = new CompanyInfoDTO();

                var globalConfig =  await _globalConfigService.GetGlobalConfigAsync();
                var aboutList = await _aboutUsService.GetAllAsync(true);
                var company = await _aboutUsService.GetByIdAsync(1);

                companyDTO = _mapper.Map<CompanyDTO>(globalConfig);
                companyAboutUsDTOs = _mapper.MapList<CompanyAboutUsDTO>(aboutList);
                companyInfo = _mapper.Map<CompanyAboutUsDTO>(company);

                if (companyDTO != null)
                {
                    var lang = _langSplitHelper.ConvertToLang(companyDTO, language);
                    var companyInfoLang = _langSplitHelper.ConvertToLang(companyInfo, language);
                    companyInfoDTO.company = lang;
                    if (companyInfoDTO.company != null && companyInfoLang != null)
                    {
                        companyInfoDTO.company["companyProfile"] = companyInfoLang["Content"];
                    }
                }

                if (companyAboutUsDTOs != null)
                {
                    var lang = _langSplitHelper.ConvertToLangList(companyAboutUsDTOs, language);
                    companyInfoDTO.abouts = lang;
                }

                return new Success(companyInfoDTO);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取Logo路径
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetLogoUrl()
        {
            try
            {
                var globalConfig = await _globalConfigService.GetGlobalConfigAsync();
                return new Success(globalConfig.LogoImages);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取地图经纬度
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetMapLatLng()
        {
            try
            {
                var globalConfig = await _globalConfigService.GetGlobalConfigAsync();
                var dto = _mapper.Map<MapLatLngDTO>(globalConfig);
                return new Success(dto);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
